package
{
	import com.util.Camera;
	
	import starling.core.Starling;
	import starling.display.Image;
	import starling.events.Event;

	public class bullet extends GameObject implements ICollidable
	{
		private var _image:Image;		
		private var _gun:GameObject;
		private var _worldX:Number;
		private var _worldY:Number;

		
		public function bullet(gun:GameObject)
		{			
			super();
			_image = new Image(Assets.getUIScreens().getTexture("fireball"));
			_gun = gun;
			addChild(_image);
			this.addEventListener(starling.events.Event.ADDED_TO_STAGE, onAddedToStage);
		}
		
		private function onAddedToStage(event:Event):void
		{			
			this.removeEventListener(starling.events.Event.ADDED_TO_STAGE, null);
			init(_gun.x+_gun.width/2, _gun.y+_gun.height/2); 			
		}
		
		public function init(xPos:Number=0, yPos:Number=0):void
		{
			_worldX = xPos + camera.getCameraPosX();
			_worldY = yPos + camera.getCameraPosY();
			
			if(!this.visible)
				this.visible = true;
			collisionmanager.addCollidable(this);
		}
		
		public override function onEnterFrame(dt:Number):void
		{
			_worldX += 500*dt; 
			
			if(World.isOffScreen(this))
			{
				dieOff();
			}			
			
			this.x = _worldX - this.camera.getCameraPosX();
			this.y = _worldY - this.camera.getCameraPosY();
		}		
		
		public function dieOff():void
		{						
			this.removeFromParent();			
			this.visible = false;	
			collisionmanager.removeCollidable(this);						
		}
		
		public function onColliding(tgt:ICollidable):void
		{
			dieOff();
		}
		
		public function get camera():Camera
		{
			return (_gun as Bunky).camera;
		}

		public function get collisionID():uint
		{
			return collisionmanager.FRIENDLY_FIRE;	
		}
		
		public override function dispose():void 
		{								
			super.dispose();
			_image = null;
		}

	}
}